﻿using Stock.Dao;
using Stock.Model.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Stock.Client.Utils
{
      public class PublicUtils
      {
            /// <summary>
            /// 反射实现两个类的对象之间相同属性的值的复制
            /// 适用于初始化新实体
            /// </summary>
            /// <typeparam name="D">返回的实体</typeparam>
            /// <typeparam name="S">数据源实体</typeparam>
            /// <param name="s">数据源实体</param>
            /// <returns>返回的新实体</returns>
            public static D CopyProperties<D, S>(S s)
            {
                  D d = Activator.CreateInstance<D>(); //构造新实例
                  try
                  {
                        var Types = s.GetType();//获得类型  
                        var Typed = typeof(D);
                        foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段  
                        {
                              foreach (PropertyInfo dp in Typed.GetProperties())
                              {
                                    if (dp.Name.ToUpper() == sp.Name.ToUpper() && dp.PropertyType == sp.PropertyType && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同  
                                    {
                                          dp.SetValue(d, sp.GetValue(s, null), null);//获得s对象属性的值复制给d对象的属性  
                                    }
                              }
                        }
                  }
                  catch (Exception ex)
                  {
                        throw ex;
                  }
                  return d;
            }

            public static string GetPrefixCode(string code)
            {
                  if ((code.StartsWith("60") || code.StartsWith("90")) && code.Length == 6)//上证AB股                  
                        code = "sh" + code;
                  else if ((code.StartsWith("000") || code.StartsWith("002") || code.StartsWith("300") || code.StartsWith("200")) && code.Length == 6)//深证AB股                  
                        code = "sz" + code;
                  else
                        code = "";
                  return code;
            }

            public static string GetPrefix(string code)
            {
                  if ((code.StartsWith("60") || code.StartsWith("90")) && code.Length == 6)//上证AB股                  
                        code = "sh";
                  else if ((code.StartsWith("000") || code.StartsWith("002") || code.StartsWith("300") || code.StartsWith("200")) && code.Length == 6)//深证AB股                  
                        code = "sz";
                  else
                        code = "";
                  return code;
            }




      }

}
